package com.hos.operation.api;

/**
 * apk 下载
 * Created by guom on 2018/11/21.
 */

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.io.IOException;

@Controller
public class ApkDownload {
    String pathRoot = "E:\\app\\";

    @RequestMapping("/download")
    public ResponseEntity<byte[]> export(String app) throws IOException {
        String filePath = pathRoot + app;
        String appName = app;
        File file = new File(filePath);
        boolean exists = file.exists();
        if (!exists) {
            String value = "来晚了，找不到了....";
            ResponseEntity re = new ResponseEntity(value.getBytes(), new HttpHeaders(), HttpStatus.OK);
            return re;
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.setContentDispositionFormData("attachment", appName);
        byte[] data = FileUtils.readFileToByteArray(file);
        return new ResponseEntity(data, headers, HttpStatus.OK);
    }


}
